package com.sicheng.algorithm.recursive.dfs;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/3/25 12:35
 */
public class 组合 {
    /**
     * 算法：找出所有相加之和为 n 的 k 个数的组合，组合中只允许含有 1 - 9 的正整数.
     * 并且每种组合中不存在重复的数字，输入: k = 3, n = 9 输出: [[1,2,6], [1,3,5], [2,3,4]]
     */

    public static void main(String[] args) throws ParseException {

        组合 solution = new 组合();
        solution.dfs(3, 6, 9, 1);
        System.out.println(solution.res);

        System.out.println(Integer.parseInt("1"));
        System.out.println(new Date(1648221267951L));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-03-25 23:14:27").getTime());
    }
    private final ArrayDeque<Integer> path = new ArrayDeque<>();
    private final List<List<Integer>> res = new ArrayList<>();
    /**
     * @param n      给定 1-n
     * @param k      选取 k 个数
     * @param target 凑出和为target
     */
    public void dfs(int k, int target, int n, int start) {
        if (path.size() == k && target == 0) {
            res.add(new ArrayList<>(path));
            return;
        }
        if (path.size() >= k && target != 0)
            return;
        for (int i = start; i <= n && i <= target && target - i >= 0; i++) {
            path.addLast(i);
            dfs(k, target - i, n, i + 1);
            path.removeLast();
        }
    }
}
